let app = new Vue({
    el: '#app',
    data() {
        return {
            selectAllName: '选择',
            onAllYes: false,
            Num_boolean: 0,
            list: [
                {
                    id: 1,
                    name: '命运的波动',
                    price: 213.8,
                    count: 1,
                    onYes: false
                },
                {
                    id: 2,
                    name: 'iPad Pro',
                    price: 5888,
                    count: 1,
                    onYes: false
                },
                {
                    id: 3,
                    name: 'MacBook Pro',
                    price: 21388,
                    count: 1,
                    onYes: false
                }
            ]
        }
    },
    computed: {
        // 计算商品价格的计算属性
        totalPrice: function () {
            let total = 0;
            let boo_num = 0;

            for (let i = 0; i < this.list.length; i++) {
                if (this.list[i].onYes) {
                    boo_num++
                    let item = this.list[i];
                    total += item.price * item.count;
                }
            }
            console.log(boo_num);
           
            if (boo_num === this.list.length) {
                this.onAllYes = true
                this.selectAllName = '全不选'
                // return 1 
            } else if (boo_num>0){
                this.onAllYes = false
                this.selectAllName = '全选'
                // return 2
            } else {
                this.selectAllName = ''
                // return 3
            }
        

            return total.toString().replace(/\B(?=(\d{3})+$)/g, ',');
        }
    },
    methods: {
        
        handleReduce: function (index) {
            console.log(index)
            if (this.list[index.count === 1]) return
            this.list[index].count--;
        },

        handleAdd: function (index) {
            // console.log(`第${index + 1}项`);
            // console.log('商品: ' + this.list[index].name + '，数量' + this.list[index].count);

            this.list[index].count++
            // this.count[index]++;
        },

        handleRemove: function (index) {
            this.list.splice(index, 1);
        },

        // 判断全选按钮是否选中
        selectAllClick: function (e) {
            let temp = true
            let checkedBoolea = e.target.checked
            for (let i = 0; i < this.list.length; i++) {
                if (checkedBoolea) {
                    this.list[i].onYes = temp
                } else {
                    this.list[i].onYes = !temp
                }
            }
        },
    },
    // 侦听器
    watch: {

            
    }
})